package edu.lhh.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.catalina.connector.Request;

import edu.lhh.model.newstype;

public class  newstypedao {
	static List<newstype> lst = new ArrayList<newstype>();
	//private int index = 0;
	
	public void add(newstype Newstype){
		/*Newstype.setIndex(index);
		lst.add(Newstype);
		index++;*/
		String sql = "INSERT INTO studen(id,name,sex,age,stf) VALUE('"+Newstype.getId() + "','"+Newstype.getName() +  "' , '" + Newstype.getSex()+"','"+Newstype.getAge()+"','"+Newstype.getStf()+"')";
		DbUtils.execute(sql);
		
	}
	
	
	public void del(int index){
		/*int i=0;
		Iterator<newstype> it = lst.iterator();
		while(it.hasNext()){
			newstype type = (newstype)it.next();
			if(type.getIndex()==index){
				lst.remove(i);
				break;
			}
			i++;
		}*/
		String sql = "DELETE FROM studen WHERE indexs = "+index;
		DbUtils.execute(sql);
		
		
		
		
		
	}
	public void updata(newstype type){
		/*Iterator<newstype> it = lst.iterator();
		int i = 0;
		while(it.hasNext()){
			newstype tp = (newstype)it.next();
			if(type.getIndex()==tp.getIndex()){
				lst.set(i, type);
			}
			i++;
		}*/
		String sql = "UPDATE studen SET id = '"+type.getId()+"' , name = '"+type.getName()+"' , sex = '"+type.getSex()+"' , age = '"+type.getAge()+"' WHERE indexs = "+type.getIndex()+"";
		DbUtils.execute(sql);
		
		
		}
		
		
		
	
	public newstype findbyid(int index) throws SQLException {
		/*Iterator<newstype> it = lst.iterator();
		while(it.hasNext()){
			newstype tp = (newstype)it.next();
			if(index == tp.getIndex()){
			type = tp;
		}
		}
		return type;*/
		
		
		newstype type = null;
		String sql = "SELECT * FROM studen WHERE indexs = "+index;
		ResultSet rs = DbUtils.query(sql);
		
		
		if(rs.next()){
			type = new newstype();
			type.setId(rs.getString("id"));
			type.setName(rs.getString("name"));
			type.setSex(rs.getString("sex"));
			type.setAge(rs.getString("age"));
			type.setIndex(rs.getInt("indexs"));
		}
		rs.close();
		
		
		
		return type;
	}
	
	
	
	public List<newstype> findall(){
		List<newstype> lst = new ArrayList<newstype>();
		ResultSet rs = DbUtils.findall("studen");
		if(rs!=null){
			try {
				while(rs.next()){
					newstype tp = new newstype();
					tp.setId(rs.getString("id"));
					tp.setName(rs.getString("name"));
					tp.setSex(rs.getString("sex"));
					tp.setAge(rs.getString("age"));
					tp.setIndex(rs.getInt("indexs"));
					lst.add(tp);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			
		}
		return lst;
	}
	public List<newstype> findbyidORname(String str) throws SQLException{
		List<newstype> lst = new ArrayList<newstype>();
		newstype type = new newstype();
		String sql = "SELECT * FROM studen WHERE id = '"+str+"' OR name = '"+str+"'";
		ResultSet rs = DbUtils.query(sql);
		if(rs.next()){
			type.setId(rs.getString("id"));
			type.setName(rs.getString("name"));
			type.setSex(rs.getString("sex"));
			type.setAge(rs.getString("age"));
			type.setIndex(rs.getInt("indexs"));	
			type.setStf(rs.getString("stf"));
			//System.out.println(rs.getString("stf"));
		}
		lst.add(type);
		newstype tpp = lst.get(0);
		return lst;
	}
	public void changet (int index){
		String sql = "UPDATE studen SET stf = 't' WHERE indexs = '"+index+"'";
		DbUtils.execute(sql);
		
	}
	public void changef (int index){
		String sql = "UPDATE studen SET stf = 'f' WHERE indexs = '"+index+"'";
		DbUtils.execute(sql);
		
	}
		
	
	
	/*public List<newstype> findallscore() {
		List<newstype> lst = new ArrayList<newstype>();
		String sql = "SELECT ";
		
		
		
		return lst;
	}
	
	*/
	
	
	

}
